//
//  MHComicSystemView.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/28.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHComicSystemView.h"
#import "NSString+Additional.h"
#import "Reachability.h"
@interface MHComicSystemView ()

@property (nonatomic, strong) UIImageView *barrayImage;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *netStateLabel;
@property(nonatomic,strong) UILabel * currentPageAndTotalPageLabel;
@property(nonatomic,strong)UILabel *episodeNameLabel;
@property(nonatomic,strong)NSString *currentAndToatalPage;

@end

@implementation MHComicSystemView

- (instancetype)init
{
    if (self = [super init]) {
        
        [[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo) name:UIDeviceBatteryStateDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
        [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(getBatteryInfo) userInfo:nil repeats:YES];
        [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(getTimeInfo) userInfo:nil repeats:YES];
        
        
        CGFloat padding = 5;
        CGFloat systemW = Height * 0.3;
        CGFloat systemX = Height - systemW;
        CGFloat systemH = 15;
        CGFloat systemY = Width - systemH;
    //显示漫画系统配置的View
        self.systemView = [[UIView alloc]initWithFrame:CGRectMake(systemX, systemY, systemW, systemH)];
        self.systemView.backgroundColor = [UIColor blackColor];
        self.systemView.alpha = 0.7;
        self.systemView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        self.systemView.center = CGPointMake(systemY + systemH * 0.5 - padding, systemW * 0.5 + padding);
    //显示电量的图片
        CGFloat barrayW = 20;
        CGFloat barrayX = systemW - barrayW - 2;
        self.barrayImage = [[UIImageView alloc]initWithFrame:CGRectMake(barrayX, 0, barrayW, systemH)];
        [self.systemView addSubview:self.barrayImage];
        CGFloat timeW = 35;
        CGFloat timeX = barrayX - timeW;
    //显示时间的Label
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(timeX, 0, timeW, systemH)];
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        self.timeLabel.textColor = [UIColor whiteColor];
        [self.systemView addSubview:self.timeLabel];
        [self getTimeInfo];
        CGFloat netW=60;
        CGFloat netX=timeX-netW;
        self.netStateLabel=[[UILabel alloc]initWithFrame:CGRectMake(netX, 0, netW, systemH)];
        self.netStateLabel.font =[UIFont systemFontOfSize:12];
        self.netStateLabel.textColor=[UIColor whiteColor];
        [self.systemView addSubview:self.netStateLabel];
        CGFloat pageW=60;
        CGFloat pageX=netX-pageW;
        self.currentPageAndTotalPageLabel=[[UILabel alloc]initWithFrame:CGRectMake(pageX, 0, pageX, systemH)];
        self.currentPageAndTotalPageLabel.font=[UIFont systemFontOfSize:13];
        self.currentPageAndTotalPageLabel.textColor=[UIColor whiteColor];
        [self.systemView addSubview:self.currentPageAndTotalPageLabel];
        CGFloat episodeW=60;
        CGFloat episodeX=pageX-episodeW;
        self.episodeNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(episodeX, 0, episodeW, systemH)];
        self.episodeNameLabel.font=[UIFont systemFontOfSize:12];
        self.episodeNameLabel.textColor=[UIColor whiteColor];
        [self.systemView addSubview:self.episodeNameLabel];
#pragma 检查是否有网络
        Reachability *r=[Reachability reachabilityWithHostname:@"www.baidu.com"];
#pragma 接收通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNetState:) name:kReachabilityChangedNotification object:nil];
        [r startNotifier];
    }
    return self;
}
-(void)changeNetState:(NSNotification *)notification
{
    switch ([notification.object currentReachabilityStatus]) {
        case 0:
           self.netStateLabel.text=@"无网络";
                   break;
          case 1:
            self.netStateLabel.text=@"蜂窝移动";
            break;
            case 2:
             self.netStateLabel.text=@"WIFI";
            break;
        default:
            break;
    }
    
}
- (void)getBatteryInfo
{
    NSString *batteryState = @"";
    switch ([UIDevice currentDevice].batteryState) {
        case 1:
            batteryState = @"Black_BatteryChargingInsides_";
            break;
        case 2:
            batteryState = @"Black_BatteryDrainingInsides_";
            break;
        case 3:
            batteryState = @"Black_BatteryDrainingInsides_";
            break;
        default:
            break;
    }
    int level = (int)[UIDevice currentDevice].batteryLevel * 100;
    self.barrayImage.tintColor = [UIColor whiteColor];
    self.barrayImage.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@%d", batteryState, level]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}

- (void)getTimeInfo
{
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString * locationString=[dateformatter stringFromDate:senddate];
    self.timeLabel.text = locationString;
}
//总章节数
-(void)setTotalPage:(NSInteger)totalPage
{
    _totalPage=totalPage;
  
   
    
}
//当前章节
-(void)setCurrentPage:(NSInteger)currentPage
{
    _currentPage=currentPage;
    
    CGFloat systemH = 15;
    CGFloat systemY = Width - systemH;
    
     self.currentAndToatalPage=[NSString stringWithFormat:@"%ld/%ld", currentPage + 1, self.totalPage];
  
    
    CGFloat episodeW=   [NSString textFrameWithFont:self.episodeNameLabel.font String:_episodeName maxSize:CGSizeMake(0, self.episodeNameLabel.frame.size.height)].width;
    
//    self.currentPageAndTotalPageLabel.frame.origin.x-episodeW-5
    self.episodeNameLabel.frame = CGRectMake(0, 0, episodeW, self.episodeNameLabel.frame.size.height);
    
    CGSize fontSize= [NSString textFrameWithFont:self.currentPageAndTotalPageLabel.font String:self.currentAndToatalPage maxSize:CGSizeMake(0, self.currentPageAndTotalPageLabel.frame.size.height)];
    
    self.currentPageAndTotalPageLabel.frame=CGRectMake(episodeW + 5, self.currentPageAndTotalPageLabel.frame.origin.y, fontSize.width, self.currentPageAndTotalPageLabel.frame.size.height);
    
    CGFloat netW =   [NSString textFrameWithFont:self.netStateLabel.font String:self.netStateLabel.text maxSize:CGSizeMake(0, self.netStateLabel.frame.size.height)].width;
    
    self.netStateLabel.frame = CGRectMake(CGRectGetMaxX(self.currentPageAndTotalPageLabel.frame) + 5, 0, netW, self.netStateLabel.frame.size.height);
    
    CGFloat timeW = 35;
    self.timeLabel.frame = CGRectMake(CGRectGetMaxX(self.netStateLabel.frame) + 5, 0, timeW, systemH);
    
    CGFloat barrayW = 20;
    self.barrayImage.frame = CGRectMake(CGRectGetMaxX(self.timeLabel.frame) + 5, 0, barrayW, systemH);
    
      self.currentPageAndTotalPageLabel.text=self.currentAndToatalPage;
    
    
    CGFloat padding = 5;
    CGFloat systemW = episodeW + netW + fontSize.width + 35 + 20 + 20;
    CGFloat systemX = Height - systemW;
    //显示漫画系统配置的View
    self.systemView.frame = CGRectMake(systemX, systemY, systemH, systemW);
    self.systemView.center = CGPointMake(systemY + systemH * 0.5 - padding, systemW * 0.5 + padding);

    
}
//当前集数的名字
-(void)setEpisodeName:(NSString *)episodeName
{
    _episodeName=episodeName;
    self.episodeNameLabel.text=episodeName;
}
@end
